﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using WFNetLib;

namespace 模拟呼叫
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
            string[] ports = SerialPort.GetPortNames();
            Array.Sort(ports);
            cbCom1.Items.Clear();
            cbCom1.Items.Add("请选择");
            cbCom1.Items.AddRange(ports);
            cbCom1.SelectedIndex = 0;

            cbCom2.Items.Clear();
            cbCom2.Items.Add("请选择");
            cbCom2.Items.AddRange(ports);
            cbCom2.SelectedIndex = 0;

            cbCom3.Items.Clear();
            cbCom3.Items.Add("请选择");
            cbCom3.Items.AddRange(ports);
            cbCom3.SelectedIndex = 0;

            cbCom4.Items.Clear();
            cbCom4.Items.Add("请选择");
            cbCom4.Items.AddRange(ports);
            cbCom4.SelectedIndex = 0;

            CallThread.AddLogEvent += new WFNetLib.intstrEventHandler(CallThread_AddLogEvent);
            Calls = new CallThread[4];
        }
        CallThread[] Calls;
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (toolStripButton1.Text == "开始")
            {
                toolStripButton1.Text = "结束";
                if(cbCom1.Text!="请选择")
                {
                    Calls[0] = new CallThread(cbCom1.Text, 1,true);
                    Calls[0].CallThreadStart();
                }
                if (cbCom2.Text != "请选择")
                {
                    Calls[1] = new CallThread(cbCom2.Text, 2, true);
                    Calls[1].CallThreadStart();
                }
                if (cbCom3.Text != "请选择")
                {
                    Calls[2] = new CallThread(cbCom3.Text, 3, false);
                    Calls[2].CallThreadStart();
                }
                if (cbCom4.Text != "请选择")
                {
                    Calls[3] = new CallThread(cbCom4.Text, 4,false);
                    Calls[3].CallThreadStart();
                }
                cbCom1.Enabled = false;
                cbCom2.Enabled = false;
                cbCom3.Enabled = false;
                cbCom4.Enabled = false;
            }
            else
            {
                toolStripButton1.Text = "开始";
                cbCom1.Enabled = true;
                cbCom2.Enabled = true;
                cbCom3.Enabled = true;
                cbCom4.Enabled = true;
            }
        }
        void CallThread_AddLogEvent(object sender, intstrEventArgs e)
        {
            this.Invoke((EventHandler)(delegate
            {
                switch (e.nArg)
                {
                    case 1:
                        if (textBox1.Text.Length > 20000)
                            textBox1.Clear();
                        textBox1.AppendText(e.strArg);
                        break;
                    case 2:
                        if (textBox2.Text.Length > 20000)
                            textBox2.Clear();
                        textBox2.AppendText(e.strArg);
                        break;
                    case 3:
                        if (textBox3.Text.Length > 20000)
                            textBox3.Clear();
                        textBox3.AppendText(e.strArg);
                        break;
                    case 4:
                        if (textBox4.Text.Length > 20000)
                            textBox4.Clear();
                        textBox4.AppendText(e.strArg);
                        break;
                }
            }));
        }
    }
}
